//
//  PaperAnswerScrollView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/3/10.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperAnswerScrollView.h"
#import "PaperOptionView.h"

@interface PaperAnswerScrollView () <PaperOptionDelegate>

@property (nonatomic, strong) YYLabel *textLabel;           // 题干
@property (nonatomic, strong) PaperOptionView *optionView;  // 选项
@property (nonatomic, strong) PaperAnswerModel *model;

@end

@implementation PaperAnswerScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigure];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupConfigure];
    }
    return self;
}

- (void)setupConfigure {
    self.backgroundColor = [UIColor whiteColor];
    //可以达到同一界面上多个控件接受事件时的排他性,从而避免一些问题。也就是说避免在一个界面上同时点击多个button。
    self.exclusiveTouch = YES;
    // 加载题目View
    self.textLabel.frame = CGRectMake(12, 6, self.width - 24.0f, 12);
    [self addSubview:self.textLabel];
    
    // 加载选项View
    self.optionView.frame = CGRectMake(0, 0, self.width, 12);
    [self addSubview:self.optionView];
    
#if TGT_XBGK
    self.textLabel.frame = CGRectMake(12, 15, self.width - 24.0f, 12);
#endif
    
    
}

- (void)refreshView {
    [self setContentOffset:CGPointZero animated:NO];
    
    // text
    self.textLabel.height = self.model.textLayout.textBoundingSize.height;
    self.textLabel.textLayout = self.model.textLayout;
    
    // option
    self.optionView.top = self.textLabel.bottom + 40;
    self.optionView.optionType = self.model.ques_model;
//    NSLog(@"%lu",(unsigned long)self.model.ques_model);
    if (self.model.ques_model == 2) {//问答
        self.optionView.answerModel = self.model;
    }else{//单选多选
        self.optionView.optionDatas = self.model.optionLayouts;
    }
    
    
    self.contentSize = CGSizeMake(SCREEN_WIDTH, self.optionView.bottom + 32.0);
}

- (void)bindModel:(PaperAnswerModel *)model {
    _model = model;
    
    [self refreshView];
}

#pragma mark - PaperOptionDelegate

- (void)sendQA_content:(NSString *)content{
    if (self.contentBlock) {
        self.contentBlock(content);
    }
    
}

- (void)PaperOptionView:(PaperOptionView *)optionView atIndex:(NSUInteger)index {
    if (self.selectBlock) {
        self.selectBlock(index);
    }
}

- (void)setOptionIndex:(NSUInteger)index withType:(PaperOptionType)type {
    [self.optionView setOptionType:type atIndex:index];
}

- (void)setAnswerContent:(NSString *)content{
    
    
    
}

#pragma mark - getter

- (YYLabel *)textLabel {
    if (_textLabel == nil) {
        _textLabel = [YYLabel new];
        _textLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        _textLabel.displaysAsynchronously = YES;
        _textLabel.ignoreCommonProperties = YES;
        _textLabel.fadeOnAsynchronouslyDisplay = NO;
        _textLabel.fadeOnHighlight = NO;
    }
    return _textLabel;
}

- (PaperOptionView *)optionView {
    if (_optionView == nil) {
        _optionView = [[PaperOptionView alloc] init];
        _optionView.delegate = self;
    }
    return _optionView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
